var lastresult = true;
var zjname = "";
var remarks = "";
var xinticode = "";
var sizeID= "";
var num = "";

var vm = new Vue({
    el : "#production",
    data : {
        record:{},
        sels:[]
    },
    init:function(){
        var _self = this;
        layui.use(['layer'], function(){
            layer = layui.layer;
        });
        this.$nextTick(function() {
            this.$http.post("../LastBottomPair/LastBottomPairList",{},{emulateJSON: true}).then(function(res){
                if(res.ok){
                    _self.tableData = res.body.JobOrderList;
                    _self.$options.methods.renderTable(_self,res.body.JobOrderList);
                }
            });
        });

    },
    methods: {
        reset:function(){
            window.location.reload();
        }
        ,vueAlert:function(msg){
            layui.use(['form'], function(){
                var layer = layui.layer;
                layer.msg(msg,{icon:6,time:2000});
            });
        },
        renderTable: function(_self,data){
            layui.use(['form','table','laydate'], function(){
                var table = layui.table;
                var form = layui.form;

                table.render({
                    elem: '#efficiencyTable'
                    , height: 'full-60'
                    , cols: [[ //标题栏
                        {fixed: 'left', type: 'numbers', title: '序号'}
                        , {field: 'JOBCODE', title: '工单号', width: 200, align: 'center'}
                        , {field: 'DDCODE', title: '大底物料编码', minWidth: 140, width: 160, align: 'center'}
                        , {field: 'DD', title: '大底名称', minWidth: 140, width: 160, align: 'center'}
                        , {field: 'CMCODE', title: '尺码', minWidth: 120, width: 100, align: 'center'}
                        , {field: 'XUANTOU', title: '楦系列', minWidth: 120, width: 100, align: 'center'}
                        , {field: 'XUANCODE', title: '楦头物料编码', minWidth: 120, width: 250, align: 'center'}
                        , {field: 'XUANTYPE', title: '楦型', minWidth: 120, width: 100, align: 'center'}
                        , {fixed: 'right', title: '操作', align: 'center', toolbar: '#LastBottomDivAction'}
                    ]]
                    , data: data
                    , page: true //是否显示分页
                    , limits: [10, 20, 50]
                    , limit: 15 //每页默认显示的数量
                });
                //监听工具条
                table.on('tool(LastBottomFilter)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                    var row = obj.data //获得当前行数据
                        ,layEvent = obj.event; //获得 lay-event 对应的值
                    if(layEvent == 'yes'){
                        _self.openConfirmReceiveWindow(row,_self);
                    }else if(layEvent == 'no'){
                        _self.refuse(row,_self,obj);
                    }
                });

            });
        }
        ,openConfirmReceiveWindow:function(data,_self){
            var indexReceiveWindow = layer.open({
                type : 1,
                title : '配对操作',
                id : 'sssss',
                shadeClose : true,
                skin: 'layui-layer-lan',
                shade : 0.8,
                area : [ '60%', '65%' ],
                content : $('#LastBottomDiv')
                ,btn: ['确认', '取消']
                ,btnAlign: 'c'
                ,yes: function(index, layero){

                    if(_self.leftShoeList == null){
                        _self.vueAlert("左脚楦头RFID未绑定！");
                        return;
                    }else if(_self.rightShoeList == null){
                        _self.vueAlert("右脚楦头RFID未绑定！");
                        return;
                    }else if(_self.leftSoleList == null){
                        _self.vueAlert("大底RFID未绑定！");
                        return;
                    }

                    if(remarks != ""){
                        _self.vueAlert("楦头大底不匹配！");
                        return;
                    }
                    var StatusUp = "CJ";
                    layer.confirm("确定绑定？", {icon: 6, title:'提示'}, function(index){
                        _self.$nextTick(function() {
                            _self.$http.post("../LastBottomPair/SaveLstBottimPariLists",{
                                leftShoeID : _self.leftShoeList.ITEMID,           //左脚楦头RFID
                                rightShoeID : _self.rightShoeList.ITEMID,         //右脚楦头RFID
                                leftsoleID : _self.leftSoleList.ITEMID,            //左脚大底RFID
                              //  leftsoleID : _self.leftSoleList.ITEMID,            //右脚大底RFID(当前不区分左右脚，作业订单所绑定大底一样)
                                rightsoleID : _self.RightSoleList.ITEMID,           //右脚大底RFID
                                planId : _self.jobPlanOrderId,
                                StatusUp : StatusUp                                  //判断该次提交为车间提交不是回流提交

                            },{emulateJSON: true}).then(function(resData){

                                layer.msg("工单完成绑定！");
                                window.setTimeout(function() {
                                    window.location.reload();
                                }, 1000);
                            });
                        });
                        layer.close(index);
                        layer.close(indexReceiveWindow);
                    });



                }
                ,btn2: function(index, layero){
                    //按钮【按钮二】的回调
                    //return false 开启该代码可禁止点击该按钮关闭
                    _self.clearReceiveForm();
                }
                ,cancel: function(){
                    //右上角关闭回调
                    //return false 开启该代码可禁止点击该按钮关闭
                    _self.clearReceiveForm();
                }
                ,success: function(layero, index){

                    $.ajax({
                        url: "../LastBottomPair/getBottimLists",
                        type: 'POST',
                        async: false,
                        data: {
                            JOBCODES: data.JOBCODE
                        },
                        dataType: 'json',
                        success: function(data) {
                            if(data.result){
                                _self.jobs = data.JobOrderPair[0];

                                _self.jobPlanOrderId = data.JobOrderPair[0].PLANID;

                                _self.shoelist = data.JobOrderPair;
                                _self.solelist = data.JobOrderSoles;
                            }else{
                                layer.msg("处理失败");
                            }
                        }
                    });

                    if(_self.jobs.REMARKS == "null"){
                        _self.jobs.REMARKS = "";
                    }

                    $("#leftShoe").val("");
                    $("#leftshoes").val("");
                    $("#rightShoe").val("");
                    $("#rightshoes").val("");
                    $("#leftsole").val("");
                    $("#leftsoles").val("");
                    $("#rightsole").val("");
                    $("#rightsoles").val("");



                    $("#JOBORDERID").val(_self.jobs.JOBCODE);
                    $("#JOBORDERORDER").val(_self.jobs.JOBCODE);
                    $("#PRODUCTCODE").val(_self.jobs.WLCODE);
                    $("#REMARKS").val(_self.jobs.REMARKS);

                }
            });
        },
        openConfirRfidWindow:function(){
            var _self = this;
            var rfid = document.getElementById("RFID").value;   //楦头RFID
            if (!(rfid && rfid.length > 0)) {
                return;
            }
            var indexReceiveWindow = layer.open({
                type : 1,
                title : '配对操作',
                id : 'sssss',
                shadeClose : true,
                skin: 'layui-layer-lan',
                shade : 0.8,
                area : [ '60%', '65%' ],
                content : $('#LastBottomDiv')
                ,btn: ['确认', '取消']
                ,btnAlign: 'c'
                ,yes: function(index, layero){

                    /*if(_self.leftShoeList == null){
                        _self.vueAlert("左脚楦头RFID未绑定！");
                        return;
                    }else if(_self.rightShoeList == null){
                        _self.vueAlert("右脚楦头RFID未绑定！");
                            return;
                    }else*/

                    if(_self.leftITMEID == null){
                        _self.vueAlert("左脚大底RFID未绑定！");
                        return;
                    }
                    if(_self.rightITMEID == null){
                        _self.vueAlert("左脚大底RFID未绑定！");
                        return;
                    }

                    var leftid = "";

                    var rightid = "";

                    if(_self.JobCodeList[0].FOOTTYPE == 'left'){
                        leftid = _self.JobCodeList[0].ITEMID;
                        rightid = _self.rightShoeList.ITEMID;
                    }else{
                        leftid = _self.leftShoeList.ITEMID;
                        rightid = _self.JobCodeList[0].ITEMID;
                    }
                    var StatusUp = "HL";
                    layer.confirm("确定绑定？", {icon: 6, title:'提示'}, function(index){
                        _self.$nextTick(function() {
                            _self.$http.post("../LastBottomPair/SaveLstBottimPariLists",{
                                leftShoeID : leftid,           //左脚楦头RFID
                                rightShoeID : rightid,         //右脚楦头RFID
                                leftsoleID : _self.leftITMEID,            //左脚大底RFID
                                rightsoleID : _self.rightITMEID,            //右脚大底RFID
                              //  leftsoleID : _self.ITMEID,            //右脚大底RFID(当前不区分左右脚，作业订单所绑定大底一样)
                                planId : _self.jobPlanOrderId,
                                StatusUp : StatusUp                     //判断该次提交为回流提交不是车间提交

                            },{emulateJSON: true}).then(function(resData){

                                layer.msg("工单完成绑定！");
                                window.setTimeout(function() {
                                window.location.reload();
                            }, 1000);
                        });
                        });
                        layer.close(index);
                        layer.close(indexReceiveWindow);
                    });



                }
                ,btn2: function(index, layero){
                    //按钮【按钮二】的回调
                    //return false 开启该代码可禁止点击该按钮关闭
                    _self.clearReceiveForm();
                }
                ,cancel: function(){
                    //右上角关闭回调
                    //return false 开启该代码可禁止点击该按钮关闭
                    _self.clearReceiveForm();
                }
                ,success: function(layero, index){


                    $.ajax({
                        url: host + "LastBottomPair/getRfidJobCode",
                        type: 'POST',
                        async: false,
                        data: {
                            rfid: rfid
                        },
                        dataType: 'json',
                        success: function(data) {
                            if(data.result){
                                if(data.results == 'false'){
                                    _self.vueAlert("RFID不存在！！");
                                    return;
                                }
                                _self.LeftRfidList =  data.leftRfidJobCode;
                                _self.RightRfidList =  data.rightRfidJobCode;
                                _self.leftITMEID =  data.leftITMEID;
                                _self.rightITMEID =  data.rightITMEID;
                                _self.remarks =  data.remarks;
                                _self.JobCodeList =  data.JobCodeLists;
                                _self.shoelist = data.JobOrderPair;
                                _self.solelist = data.JobOrderSoles;
                                _self.jobPlanOrderId = data.JobOrderPair[0].PLANID;


                            }else{
                                layer.msg("处理失败");
                            }
                        }
                    });


                    if(_self.JobCodeList == undefined){
                        _self.vueAlert("RFID不存在！！");
                        return;
                    }

                    if(_self.JobCodeList[0].REMARKS == " "){
                        _self.JobCodeList[0].REMARKS = "";
                    }

                    if(_self.JobCodeList[0].FOOTTYPE == "left"){
                        $("#leftShoe").val(rfid);
                        $("#leftshoes").val(_self.JobCodeList[0].XTVALUE);
                    }else{
                        $("#rightShoe").val(rfid);
                        $("#rightshoes").val(_self.JobCodeList[0].XTVALUE);
                    }


                    $("#leftsole").val(_self.LeftRfidList);
                    $("#rightsole").val(_self.RightRfidList);
                    if(_self.remarks == null){
                        _self.remarks = "大底线边库无匹配大底！！";
                    }
                    $("#leftsoles").val(_self.remarks);
                    $("#rightsoles").val(_self.remarks);
                    /*$("#rightsole").val("");
                    $("#rightsoles").val("");*/



                    $("#JOBORDERID").val(_self.JobCodeList[0].ZYCODE);
                    $("#JOBORDERORDER").val(_self.JobCodeList[0].ZYCODE);
                    $("#PRODUCTCODE").val(_self.JobCodeList[0].WLVALUE);
                    $("#REMARKS").val(_self.JobCodeList[0].REMARKS);

                }
            });
        },
        //左脚楦头触发JS 1
        getleftRfidList: function(event){
            var _self = this;

            var leftShoe = document.getElementById("leftShoe").value;   //左脚楦头RFID

            if (!(leftShoe && leftShoe.length > 10)) {
                return;
            }
            var  ss = leftShoe.substr(leftShoe.length-4,1);

            if(ss != 'L'){
                remarks = "请录入左脚楦头RFID！";
                _self.vueAlert(remarks);
                return;
            }

            for(o = 0; o<_self.shoelist.length ; o++){
                if(_self.shoelist[o].TYPE == 'LEFT'){
                    xinticode = _self.shoelist[o].SHOEID;
                    sizeID = _self.shoelist[o].CMID;
                }else if(_self.shoelist[o].TYPE == 'LEFT-RIGHT'){
                    xinticode = _self.shoelist[o].SHOEID;
                    sizeID = _self.shoelist[o].CMID;
                }
            }

            var data ={
                batch_no : leftShoe
            };
            _self.$nextTick(function() {
                _self.$http.post("../LastBottomPair/getShoeResult",data,{emulateJSON: true}).then(function(res){
                    if(res.ok){
                        _self.leftShoeList = res.body.ShoeLists[0];

                        if(res.body.ShoeLists.length == 0){
                            remarks = "左脚楦头RFID在系统中不存在！";
                            _self.vueAlert(remarks);
                            document.getElementById("leftshoes").value = remarks;
                            return;
                        }else{
                            if(_self.leftShoeList.WLCODE != xinticode ){
                                remarks = "左脚楦头RFID和该工单楦头不匹配！！";
                                _self.vueAlert(remarks);
                                document.getElementById("leftshoes").value = remarks;
                                return;
                            }else if(_self.leftShoeList.CMID !=  sizeID){
                                remarks = "左脚楦头RFID型体尺码和该工单型体尺码不对应！";
                                _self.vueAlert(remarks);
                                document.getElementById("leftshoes").value = remarks;
                                return;
                            }
                        }

                        document.getElementById("leftshoes").value = _self.leftShoeList.WLVALUE;
                        remarks ="";
                    }
                });
            });


        },

        //右脚楦头出发JS 2
        getrightRfidList: function(event){
            var _self = this;

            var rightShoe = document.getElementById("rightShoe").value;   //右脚楦头RFID

            //if ( event.keyCode != 13 || !(rightShoe && rightShoe.length > 0)) {
            if (!(rightShoe && rightShoe.length > 10)) {
                return;
            }

            var  sss = rightShoe.substr(rightShoe.length-4,1);

            if(sss != 'R'){
                remarks = "请录入右脚楦头RFID！";
                _self.vueAlert(remarks);
                return;
            }

            for(o = 0; o< _self.shoelist.length ; o++){
                if(_self.shoelist[o].TYPE == 'RIGHT'){
                    xinticode = _self.shoelist[o].SHOEID;
                    sizeID = _self.shoelist[o].CMID;
                }else if(_self.shoelist[o].TYPE == 'LEFT-RIGHT'){
                    xinticode = _self.shoelist[o].SHOEID;
                    sizeID = _self.shoelist[o].CMID;
                }
            }

            var data ={
                batch_no : rightShoe
            };
            _self.$nextTick(function() {
                _self.$http.post("../LastBottomPair/getShoeResult",data,{emulateJSON: true}).then(function(res){
                    if(res.ok){
                        _self.rightShoeList = res.body.ShoeLists[0];

                        if(res.body.ShoeLists.length == 0){
                            remarks = "右脚楦头RFID在系统中不存在！";
                            _self.vueAlert(remarks);
                            document.getElementById("rightshoes").value = remarks;
                            return;
                        }else{
                            if(_self.rightShoeList.WLCODE != xinticode){
                                remarks = "右脚楦头RFID型体和该工单型体不对应！";
                                _self.vueAlert(remarks);
                                document.getElementById("rightshoes").value = remarks;
                                return;
                            }else if(_self.rightShoeList.CMID != sizeID){
                                remarks = "右脚楦头RFID型体尺码和该工单型体尺码不对应！";
                                _self.vueAlert(remarks);
                                document.getElementById("rightshoes").value = remarks;
                                return;
                            }
                        }

                        document.getElementById("rightshoes").value = _self.rightShoeList.WLVALUE;
                        remarks ="";
                    }
                });
            });


        },

        /*//大底触发JS
        getSoleList: function(event){
            var _self = this;

            var leftsole = document.getElementById("leftsole").value;   //左脚大底RFID

            if ( !(leftsole && leftsole.length > 10)) {
                return;
            }

            for(o = 0; o<_self.solelist.length ; o++){
                if(_self.solelist[o].TYPE == 'LEFT'){
                    xinticode = _self.solelist[o].SOLEWLID;
                    sizeID = _self.solelist[o].CMID;
                }else if(_self.solelist[o].TYPE == 'LEFT-RIGHT'){
                    xinticode = _self.solelist[o].SOLEWLID;
                    sizeID = _self.solelist[o].CMID;
                }
            }

            var data ={
                batch_no : leftsole
            };
            _self.$nextTick(function() {
                _self.$http.post("../LastBottomPair/getShoeResult",data,{emulateJSON: true}).then(function(res){
                    if(res.ok){
                        _self.leftSoleList = res.body.ShoeLists[0];

                        if(res.body.ShoeLists.length == 0){
                            remarks = "大底RFID在系统中不存在！";
                            _self.vueAlert(remarks);
                            document.getElementById("leftsoles").value = remarks;
                            return;
                        }else{
                            if(_self.leftSoleList.WLVALUE != xinticode ){
                                remarks = "大底和该工单不对应！！";
                                _self.vueAlert(remarks);
                                document.getElementById("leftsoles").value = remarks;
                                return;
                            }else if(_self.leftSoleList.CMID !=  sizeID){
                                remarks = "大底尺码和该工单尺码不对应！";
                                _self.vueAlert(remarks);
                                document.getElementById("leftsoles").value = remarks;
                                return;
                            }else if(_self.leftSoleList.STATUS == "OK"){
                                remarks = "大底已绑定其他工单！";
                                _self.vueAlert(remarks);
                                document.getElementById("leftsoles").value = remarks;
                                return;
                            }
                        }
                        document.getElementById("leftsoles").value = _self.leftSoleList.WLVALUE;
                        remarks ="";
                    }
                });
            });


        },*/

        //左脚大底触发JS 3
        getleftsoleList: function(event){
            var _self = this;

            var leftsole = document.getElementById("leftsole").value;   //左脚大底RFID

            if (!(leftsole && leftsole.length > 0)) {
                return;
            }
            var  ss = leftsole.substr(leftsole.length-3,2);

            if(ss != '-L'){
                remarks = "请录入左脚大底RFID！";
                _self.vueAlert(remarks);
                return;
            }

            for(o = 0; o<_self.solelist.length ; o++){
                if(_self.solelist[o].TYPE == 'LEFT'){
                    xinticode = _self.solelist[o].SOLEWLID;
                    sizeID = _self.solelist[o].CMID;
                }else if(_self.solelist[o].TYPE == 'LEFT-RIGHT'){
                    xinticode = _self.solelist[o].SOLEWLID;
                    sizeID = _self.solelist[o].CMID;
                }
            }

            var data ={
                batch_no : leftsole
            };
            _self.$nextTick(function() {
                _self.$http.post("../LastBottomPair/getShoeResult",data,{emulateJSON: true}).then(function(res){
                    if(res.ok){
                        _self.leftSoleList = res.body.ShoeLists[0];

                        if(res.body.ShoeLists.length == 0){
                            remarks = "左脚大底RFID在系统中不存在！";
                            _self.vueAlert(remarks);
                            document.getElementById("leftsoles").value = remarks;
                            return;
                        }else{
                            if(_self.leftSoleList.WLVALUE != xinticode ){
                                remarks = "左脚大底RFID型体和该工单型体不对应！！";
                                _self.vueAlert(remarks);
                                document.getElementById("leftsoles").value = remarks;
                                return;
                            }else if(_self.leftSoleList.CMID !=  sizeID){
                                remarks = "左脚大底RFID型体尺码和该工单型体尺码不对应！";
                                _self.vueAlert(remarks);
                                document.getElementById("leftsoles").value = remarks;
                                return;
                            }else if(_self.leftSoleList.STATUS == "OK"){
                                remarks = "左脚大底RFID已绑定其他工单！";
                                _self.vueAlert(remarks);
                                document.getElementById("leftsoles").value = remarks;
                                return;
                            }
                        }
                        document.getElementById("leftsoles").value = _self.leftSoleList.WLVALUE;
                    }
                });
            });


        },

        //右脚大底触发JS
        getrightsoleList: function(event){
            var _self = this;

            var rightsole = document.getElementById("rightsole").value;   //右脚大底RFID

            if ( !(rightsole && rightsole.length > 0)) {
                return;
            }
            var  ss = rightsole.substr(rightsole.length-3,2);

            if(ss != '-R'){
                remarks = "请录入右脚大底RFID！";
                _self.vueAlert(remarks);
                return;
            }

            for(o = 0; o<_self.solelist.length ; o++){
                if(_self.solelist[o].TYPE == 'LEFT'){
                    xinticode = _self.solelist[o].SOLEWLID;
                    sizeID = _self.solelist[o].CMID;
                }else if(_self.solelist[o].TYPE == 'LEFT-RIGHT'){
                    xinticode = _self.solelist[o].SOLEWLID;
                    sizeID = _self.solelist[o].CMID;
                }
            }

            var data ={
                batch_no : rightsole
            };
            _self.$nextTick(function() {
                _self.$http.post("../LastBottomPair/getShoeResult",data,{emulateJSON: true}).then(function(res){
                    if(res.ok){
                        _self.RightSoleList = res.body.ShoeLists[0];

                        if(res.body.ShoeLists.length == 0){
                            remarks = "右脚大底RFID在系统中不存在！";
                            _self.vueAlert(remarks);
                            document.getElementById("rightsoles").value = remarks;
                            return;
                        }else{
                            if(_self.RightSoleList.WLVALUE != xinticode ){
                                remarks = "右脚大底RFID型体和该工单型体不对应！！";
                                _self.vueAlert(remarks);
                                document.getElementById("rightsoles").value = remarks;
                                return;
                            }else if(_self.RightSoleList.CMID !=  sizeID){
                                remarks = "右脚大底RFID型体尺码和该工单型体尺码不对应！";
                                _self.vueAlert(remarks);
                                document.getElementById("rightsoles").value = remarks;
                                return;
                            }else if(_self.RightSoleList.STATUS == "OK"){
                                remarks = "右脚大底RFID已绑定其他工单！";
                                _self.vueAlert(remarks);
                                document.getElementById("rightsoles").value = remarks;
                                return;
                            }
                        }
                        document.getElementById("rightsoles").value = _self.RightSoleList.WLVALUE;
                    }
                });
            });


        }

        ,clearReceiveForm:function(){
            $("#reMakeNum").val('');
            $("#confirmReceiveNum").val('');
        }
    }
});

var rfidObj = null;
var changeFun = null;
function setReadRfid(obj) {
    rfidObj = obj;
    changeFun = rfidObj.getAttribute("changeFun");
}
function blurObj() {
    rfidObj = null;
}
function getLocalhostRfid() {
    if (!rfidObj) {
        window.setTimeout("getLocalhostRfid()", 300);
        return;
    }
    $.post(host + "rfid/getAndroidRfid", {}, function(res) {
        try {
            if (res.result) {
                _read_rfid_conn_err = true;
                var data = res.data;
                var obj = JSON.parse(data);

                if (obj.data) {
                    $(rfidObj).val(obj.data);
                    eval(changeFun);

                    //扫描到RFID后5秒之后再重新扫描
                    window.setTimeout("getLocalhostRfid()", 5000);
                } else {
                    window.setTimeout("getLocalhostRfid()", 300);
                }
            } else {
                if (!_read_rfid_conn_err) {
                    top.layer.msg(res.msg);
                }
                if (res.msg.indexOf("Connection refused") > -1) {
                    _read_rfid_conn_err = true;
                }
                //5秒后继续读取RFID
                window.setTimeout("getLocalhostRfid()", 5000);
            }
        } catch (e) {
            window.setTimeout("getLocalhostRfid()", 1000);
        }
    }, "json");
}
getLocalhostRfid();
